//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: "",
    imgUrl: baseJs.imgUrl,
    pageIndex: 1,
    listData: [],
    state: '',
    // 添加小体验店
    addSmallFlag: true,
    phone: "",
    receivePhone: "",
    // 处理
    dealModalFlag: true,
    smallId: "",
    addOrReduce: "请填写添加的应还额度",
    dealType: "",
    dealM: "",
    dealRemark: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
    })
    this.AgentHeHuoRenGetSmallExperienceShopList();
  },

  

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.pageIndex++;
    this.AgentHeHuoRenGetSmallExperienceShopList();
  },

  // 
  AgentHeHuoRenGetSmallExperienceShopList: function() {
    var that = this;
    var uid = this.data.uid;
    var pageIndex = this.data.pageIndex;
    var listData = this.data.listData;
    if (pageIndex == 1) {
      wx.showLoading({
        title: '加载中',
      })
    }
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=AgentHeHuoRenGetSmallExperienceShopList&UID=" + uid + "&PageSize=10&PageIndex=" + pageIndex),
      success: function(res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        for (var i = 0; i < json.length; i++) {
          json[i].AllBorrowMa = parseFloat(json[i].AllBorrowMa).toFixed(2);
          json[i].AllReturnMa = parseFloat(json[i].AllReturnMa).toFixed(2);
          json[i].active = false;
          listData.push(json[i])
        }
        that.setData({
          listData: listData,
        })
        console.log(listData);
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
        wx.hideLoading();
      }
    })
  },
  // 
  addSmallStore: function() {
    this.setData({
      addSmallFlag: false,
    })
  },
  // 
  phone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 
  receivePhone: function(e) {
    this.setData({
      receivePhone: e.detail.value
    })
  },
  // addSmallConfirm
  addSmallConfirm: function() {
    var that = this;
    var uid = this.data.uid;
    var phone = this.data.phone;
    var receivePhone = this.data.receivePhone;
    if (phone == '') {
      wx.showToast({
        title: "请填写小体验店账号！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (receivePhone == '') {
      wx.showToast({
        title: "请填写小体验店的奖励接收账号！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=AgentHeHuoRenAddSmallExperienceShop&UID=" + uid + " &Phone=" + phone + "&ReceiveRewardPhone=" + receivePhone),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          that.setData({
            pageindex: 1,
            listData: [],
            state: "",
            addSmallFlag: true,
          })
          that.AgentHeHuoRenGetSmallExperienceShopList();
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // addSmallCancel
  addSmallCancel: function() {
    this.setData({
      addSmallFlag: true,
    })
  },
  // 点击处理
  dealTap: function(e) {
    var that = this;
    var listData = this.data.listData;
    for (var i = 0; i < listData.length; i++) {
      listData[i].active = false;
    }
    listData[e.currentTarget.dataset.index].active = true;
    this.setData({
      listData: listData,
      smallId: e.currentTarget.dataset.smallid,
    })
  },
  //添加分配
  add: function() {
    this.setData({
      dealModalFlag: false,
      addOrReduce: "请填写添加的应还额度",
      dealType: "1",
    })
  },
  // 减少分配
  reduce: function() {
    this.setData({
      dealModalFlag: false,
      addOrReduce: "请填写减少的应还额度",
      dealType: "2",
    })
  },
  // 
  dealM: function(e) {
    this.setData({
      dealM: e.detail.value,
    })
  },
  // 
  dealRemark: function(e) {
    this.setData({
      dealRemark: e.detail.value,
    })
  },
  //  
  dealModalCancel: function() {
    this.setData({
      dealModalFlag: true,
    })
  },
  // 
  dealModalConfirm: function() {
    var that = this;
    var uid = this.data.uid;
    var smallId = this.data.smallId;
    var dealM = this.data.dealM;
    var dealRemark = this.data.dealRemark;
    var dealType = this.data.dealType;
    var listData = this.data.listData;
    if (dealM == ''){
      wx.showToast({
        title: "请填写额度！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    console.log("method=AgentHeHuoRenSetGoodsMaToSmallExperienceShop&UID=" + uid + "&SetGoodsMa=" + dealM + "&Remark=" + dealRemark + "&FlowType=" + dealType + "&SmallExperienceShopId=" + smallId);
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=AgentHeHuoRenSetGoodsMaToSmallExperienceShop&UID=" + uid + "&SetGoodsMa=" + dealM + "&Remark=" + dealRemark + "&FlowType=" + dealType + "&SmallExperienceShopId=" + smallId),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          for (var i = 0; i < listData.length; i++) {
            if (listData[i].SmallExperienceShopId == smallId && dealType == '1') {
              listData[i].AllBorrowMa = parseFloat(Number(listData[i].AllBorrowMa) + Number(dealM)).toFixed(2);
            }
            if (listData[i].SmallExperienceShopId == smallId && dealType == '2') {
              listData[i].AllBorrowMa = parseFloat(Number(listData[i].AllBorrowMa) - Number(dealM)).toFixed(2);
            }
            listData[i].active = false;
          }
          that.setData({
            listData: listData,
            dealModalFlag: true,
          })

        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // 

})